package com.DAO;

import java.util.List;

import org.hibernate.Session;

public class DAO<T> {

    private Class<?> persistentClass;
    protected Session session;

    public DAO(Session session, Class<?> persistentClass) {
        this.session = session;
        this.persistentClass = persistentClass;
    }

    protected Session getSession(){
		return session; 	
	}
    
    @SuppressWarnings("unchecked")
    public T load(Long id) {
        System.out.println("lendo " + persistentClass + " com id " + id);
        return (T) session.load(persistentClass, id);
    }
   
    public void save(T t) {
    	System.out.println("salvando  " + t);
    	session.beginTransaction().begin();
    	session.beginTransaction().commit();
    	session.save(t);
    }
    
	public void delete(T t) {
		System.out.println("Deletando " + t);
		session.delete(t);
	}
	
    @SuppressWarnings("unchecked")
	public List<T> list() {
    	System.out.println("Listando todos");
    	return session.createCriteria(persistentClass).list();
    }

    public void merge(T t) {
    	System.out.println("Salvando ou atualizando " + t);
        session.merge(t);
    }
}